package com.briup.bookstore.service.impl;

import com.briup.bookstore.common.security.SecurityContext;
import com.briup.bookstore.domain.dto.AddressModifyDTO;
import com.briup.bookstore.domain.vo.AddressVO;
import com.briup.bookstore.mapper.AddressMapper;
import com.briup.bookstore.service.AddressService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class AddressServiceImpl implements AddressService {

    private final AddressMapper addressMapper;

    @Override
    public List<AddressVO> listByUserId() {
        Integer userId = SecurityContext.userId();
        return addressMapper.listByUserId(userId);
    }

    @Override
    public void addAddress(AddressModifyDTO modifyDTO) {
        // 在使用了参数校验后，参数校验就在Controller层生效了
        // 所以在Service层就不需要再进行参数校验了
        modifyDTO.setUserId(SecurityContext.userId());
        addressMapper.insertAddress(modifyDTO);
    }

    @Override
    public void updateAddress(AddressModifyDTO modifyDTO) {
        modifyDTO.setUserId(SecurityContext.userId());
        addressMapper.updateAddress(modifyDTO);
    }
}
